package com.xywy.tangandroid.util;
import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.DecimalFormat;

import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.util.Log;

/**
 * �ļ�������
 * @author spring sky
 *
 */
public class FileUtil {
	 /**
     * ��ȡĿ¼����
     * @param url
     * @return FileName
     */
	static String Tag= "FileUtil";
    public static String getFileName(String url)
    {
        int lastIndexStart = url.lastIndexOf("/");
        if(lastIndexStart!=-1)
        {
            return url.substring(lastIndexStart+1, url.length());
        }else{
            return new Timestamp(System.currentTimeMillis()).toString();
        }
    }
    
    
    /**
	 * �����ļ�
	 * 
	 * @param extension
	 * @return
	 */
	public static File generateFile(String extension) {
		    File dir = null;
		    String BASE_DIR = Environment.getExternalStorageDirectory() + "/"
					+ "com.xyws.Tang";// �����ļ���Ŀ¼
			dir = new File(BASE_DIR+"/TangDoctor" + "/image");
		
		if (!dir.exists()){
//			dir.getParentFile().mkdirs();
			dir.mkdirs(); // ��bug,����û���ĳ��Ŀ¼ɾ����,�ᱨ��
		}
		File file = new File(dir, System.currentTimeMillis() + "." + extension);

		if (!file.exists()){
			try {
				file.createNewFile();
				return file;
			} catch (IOException e) {
				e.printStackTrace();
				LogUtil.i(Tag, "�������յ���Ƭ�쳣");
			}
			}
		return null;
	}
    
    
    /**
     * �ļ��Ƿ����
     * @param path
     * @return
     */
    public static boolean FileIsExist(String path) {
        File file = new File(path);

        return file.exists();
    }
    
    public static boolean hasExternalCacheDir() {
		return Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO;
	}
    /**
	 * Get the external app cache directory.
	 * 
	 * @param context
	 *            The context to use
	 * @return The external cache dir
	 */
	public static File getCacheDir(final Context context) {
		if (hasExternalCacheDir())
//			return context.getExternalCacheDir();
			return context.getCacheDir();

		// Before Froyo we need to construct the external cache dir ourselves
		final String cacheDir = "/Android/data/" + context.getPackageName()
				+ "/cache/";
		return new File(Environment.getExternalStorageDirectory().getPath()
				+ cacheDir);
	}
	/**
     * �ж�SD���Ƿ����
     * @return boolean
     */
    public static boolean checkSDCard() {
        if (android.os.Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED)) {
            return true;
        } else {
            return false;
        }
    }
    /**
     * �����˳���·����ָ����Ŀ¼���������б��赫�����ڵĸ�Ŀ¼
     * @param dirPath
     * @return
     */
    public static File createFileDirs(String dirPath) {
        File dir = new File(dirPath);
        dir.mkdirs();

        return dir;
    }
    /**
     * �����ļ���ָ��Ŀ¼
     * @param context
     * @return  �ļ������Ŀ¼
     */
    public static String setMkdir(Context context)
    {
        String filePath = null;
        if(checkSDCard())
        {
            filePath = Environment.getExternalStorageDirectory()+File.separator+"wanbu"+File.separator+"downloads";
        }else{
            filePath = context.getCacheDir().getAbsolutePath()+File.separator+"wanbu"+File.separator+"downloads";
        }
        File file = new File(filePath);
        if(!file.exists())
        {
            file.mkdirs();
            Log.e("file", "Ŀ¼������   ����Ŀ¼ ");
        }else{
        	Log.e("file", "Ŀ¼����");
        }
        return filePath;
    }
    
    /**
     * ��ȡ·��
     * @return
     * @throws IOException
     */
    public static  String getPath(Context context,String url)
    {
        String path = null;
        try {
            path = FileUtil.setMkdir(context)+File.separator+url.substring(url.lastIndexOf("/")+1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return path;
    }
    
    /**
     * ��ȡ�ļ��Ĵ�С
     * 
     * @param fileSize
     *            �ļ��Ĵ�С
     * @return
     */
    public static String FormetFileSize(int fileSize) {// ת���ļ���С
        DecimalFormat df = new DecimalFormat("#.00");
        String fileSizeString = "";
        if (fileSize < 1024) {
            fileSizeString = df.format((double) fileSize) + "B";
        } else if (fileSize < 1048576) {
            fileSizeString = df.format((double) fileSize / 1024) + "K";
        } else if (fileSize < 1073741824) {
            fileSizeString = df.format((double) fileSize / 1048576) + "M";
        } else {
            fileSizeString = df.format((double) fileSize / 1073741824) + "G";
        }
        return fileSizeString;
    }
    /**
     * ɾ����Ŀ¼�µ�ָ���ļ�
     * @param filePath �ļ�Ŀ¼
     * @param fileName �ļ���
     * @return
     */
    public static boolean deleteFile(String filePath, String fileName) {
        if (FileIsExist(filePath + fileName)) {
            File file = new File(filePath + fileName);

            return file.delete();
        }

        return true;
    }

    /**
     * ɾ����·��ָ�����ļ�
     * @param path �ļ�·��
     * @return
     */
    public static boolean deleteFile(String path) {
        if (FileIsExist(path)) {
            File file = new File(path);

            return file.delete();
        }

        return true;
    }
    /**
     * ɾ�������ļ�
     * @param   sPath    ��ɾ���ļ����ļ���
     * @return �����ļ�ɾ���ɹ�����true�����򷵻�false
     */
    public static boolean deleteFile1(String sPath) {
        boolean flag = false;
        File file = new File(sPath);
     // ·��Ϊ�ļ��Ҳ�Ϊ�������ɾ��
        if (file.isFile() && file.exists()) {
            file.delete();
            flag = true;
        }
        return flag;
    }
    /**
     * ɾ��Ŀ¼���ļ��У��Լ�Ŀ¼�µ��ļ�
     * @param   sPath ��ɾ��Ŀ¼���ļ�·��
     * @return  Ŀ¼ɾ���ɹ�����true�����򷵻�false
     */
    public static boolean deleteDirectory(String sPath) {
    	//���sPath�����ļ��ָ�����β���Զ�����ļ��ָ���
        if (!sPath.endsWith(File.separator)) {
            sPath = sPath + File.separator;
        }
        File dirFile = new File(sPath);
      //���dir��Ӧ���ļ������ڣ����߲���һ��Ŀ¼�����˳�
        if (!dirFile.exists() || !dirFile.isDirectory()) {
            return false;
        }
       boolean  flag = true;
     //ɾ���ļ����µ������ļ�(������Ŀ¼)
        File[] files = dirFile.listFiles();
        for (int i = 0; i < files.length; i++) {
        	//ɾ�����ļ�
            if (files[i].isFile()) {
                flag = deleteFile1(files[i].getAbsolutePath());
                if (!flag) break;
            } //ɾ����Ŀ¼
            else {
                flag = deleteDirectory(files[i].getAbsolutePath());
                if (!flag) break;
            }
        }
        if (!flag) return false;
      //ɾ����Ŀ¼
        if (dirFile.delete()) {
            return true;
        } else {
            return false;
        }
    }
    
    
}